package com.yksj.healthtalk.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.yksj.healthtalk.entity.DoctorServiceContentEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.doctorstation.DoctorServiceHistoryClientListActivity;
import com.yksj.healthtalk.ui.doctorstation.DoctorServiceHistoryListActivity;

public class DoctorServiceContentListAdapter extends BaseAdapter{
		public List<DoctorServiceContentEntity> entities=new ArrayList<DoctorServiceContentEntity>();
		private final LayoutInflater inflater;
		OnClickDeleteListener mOnClickListener;
		private final  Context context;
		private int type;//type=1表示进入 已删除服务内容页面
		public interface OnClickDeleteListener{
			void onclick(DoctorServiceContentEntity id);
		}
		public DoctorServiceContentListAdapter(List<DoctorServiceContentEntity> entities,OnClickListener clickListener,Context mconContext,int type){
			this.entities=entities;
			inflater=LayoutInflater.from(mconContext);
			context=mconContext;
			this.type=type;
			this.mOnClickListener=(OnClickDeleteListener) clickListener;
		}
		public void onBoundData(List<DoctorServiceContentEntity> entities) {
			this.entities.clear();
			this.entities.addAll(entities);
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return entities.size();
		}

		@Override
		public Object getItem(int position) {
			return entities.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final DoctorServiceContentEntity entity = entities.get(position);
			final ViewHolder holder;
			if(convertView==null){
				holder=new ViewHolder();
				convertView=inflater.inflate(R.layout.doctor_service_content_item, null);
				holder.name=(TextView) convertView.findViewById(R.id.name);
				holder.content=(TextView) convertView.findViewById(R.id.content);
				holder.delete=(Button) convertView.findViewById(R.id.delete);
				holder.isChange=(CheckBox) convertView.findViewById(R.id.check);
				holder.contentChange=(CheckBox)convertView.findViewById(R.id.check_content);
				holder.button_1=(Button) convertView.findViewById(R.id.button_1);
				holder.button_2=(Button) convertView.findViewById(R.id.button_2);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			
			holder.name.setText(entity.getSERVICE_CONTENT());
			holder.content.setText(entity.getSERVICE_DESC());
			if(type==0){
				if("System".equals(entity.getSERVICE_TYPE())){
					holder.delete.setVisibility(View.GONE);
				}else{
					holder.delete.setVisibility(View.VISIBLE);
				}
				
				holder.delete.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if(mOnClickListener!=null)mOnClickListener.onclick(entity);
					}
				});
			}else{//
//				holder.delete.setBackgroundDrawable(null);
//				holder.delete.setText("2013-12-30 17:50 ");
				holder.delete.setVisibility(View.GONE);
			}
			
			
			holder.isChange.setOnCheckedChangeListener(null);
			if(!"2".equals(entity.getNECESSARY_FLAG())){
				/*holder.isChange.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						if(isChecked){
							entity.setISSELECT("1");
//							holder.isChange.setChecked(true);
						}else{
							entity.setISSELECT("0");
//							holder.isChange.setChecked(false);
						}
					}
				});	*/
				holder.isChange.setEnabled(true);
				holder.isChange.setFocusable(true);
				holder.isChange.setBackgroundResource(R.drawable.check_xunze);
			}else{
				holder.isChange.setBackgroundResource(R.drawable.default_check);
				entity.setISSELECT("1");
				holder.isChange.setChecked(true);
				holder.isChange.setEnabled(false);
				holder.isChange.setFocusable(false);
			}
			
			//患者列表
			holder.button_1.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
				Intent	intent=new Intent(context,DoctorServiceHistoryClientListActivity.class);
				intent.putExtra("SERVICE_CONTENT_ID",entity.getSERVICE_CONTENT_ID() );
				context.startActivity(intent);
				}
			});
			//服务历史
			holder.button_2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent=new Intent(context,DoctorServiceHistoryListActivity.class);
					intent.putExtra("SERVICE_CONTENT_ID",entity.getSERVICE_CONTENT_ID() );
					context.startActivity(intent);
				}
			});
			
			if("1".equals(entity.getISSELECT())||"2".equals(entity.getNECESSARY_FLAG())){
				holder.isChange.setChecked(true);
			}else{
				holder.isChange.setChecked(false);
			}
			
			if(!"2".equals(entity.getNECESSARY_FLAG())){
				holder.isChange.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						if(isChecked){
							entity.setISSELECT("1");
//							holder.isChange.setChecked(true);
						}else{
							entity.setISSELECT("0");
//							holder.isChange.setChecked(false);
						}
					}
				});	
			}
			
			return convertView;
		}
		
		private class ViewHolder{
			private TextView name;
			private TextView content;
			private CheckBox isChange;
			private CheckBox contentChange;
			private Button delete;
			private Button button_1;
			private Button button_2;
		}
		
	}